<template>
  <div class="patrol-route-container">
    <div class="search-bar">
      <div class="search-section">
        <div class="search-inputs">
          <el-select
            v-model="queryParams.communityId"
            placeholder="请选择社区"
            clearable
            class="search-item"
            @change="handleCommunityChange"
          >
            <el-option
              v-for="item in communityOptions"
              :key="item.value"
              :label="item.label"
              :value="item.value"
            ></el-option>
          </el-select>
          <el-input
            v-model="queryParams.searchText"
            placeholder="请输入巡更路线"
            class="search-item search-input"
            @keyup.enter.native="searchData"
          >
            <el-button
              slot="append"
              icon="el-icon-search"
              @click="searchData"
            ></el-button>
          </el-input>
          <div class="search-buttons">
            <el-button
              type="info"
              plain
              icon="el-icon-refresh"
              @click="resetSearch"
              >重置</el-button
            >
            <el-button type="primary" icon="el-icon-search" @click="searchData"
              >搜索</el-button
            >
          </div>
        </div>
      </div>
      <div class="add-section">
        <el-button
          type="primary"
          icon="el-icon-plus"
          @click="handleAdd"
          class="add-button"
          >添加巡更路线</el-button
        >
      </div>
    </div>

    <div class="table-container">
      <el-table
        :data="tableData"
        border
        style="width: 100%"
        v-loading="loading"
      >
        <el-table-column label="序号" width="80" align="center">
          <template slot-scope="scope">
            {{ (queryParams.pageNum - 1) * queryParams.pageSize + scope.$index + 1 }}
          </template>
        </el-table-column>
        
        <el-table-column
          prop="communityName"
          label="所属社区"
          width="120"
          align="center"
        ></el-table-column>
        
        <el-table-column
          prop="routeName"
          label="巡更路线"
          width="150"
          align="center"
        ></el-table-column>
        
        <el-table-column
          prop="remark"
          label="备注"
          width="180"
          align="center"
        ></el-table-column>
        
        <el-table-column
          prop="photoRequired"
          label="是否拍照"
          width="120"
          align="center"
        >
          <template slot-scope="scope">
            <span
              :class="
                scope.row.isPhotograph === 1
                  ? 'photo-required'
                  : 'photo-not-required'
              "
            >
              {{ scope.row.isPhotograph === 1 ? "需要" : "不需要" }}
            </span>
          </template>
        </el-table-column>
        
        <el-table-column prop="status" label="状态" width="100" align="center">
          <template slot-scope="scope">
            <span
              :class="
                scope.row.status === 1 ? 'status-normal' : 'status-disabled'
              "
            >
              {{ scope.row.status === 1 ? "启用" : "禁用" }}
            </span>
          </template>
        </el-table-column>
        
        <el-table-column label="操作" min-width="250" align="center">
          <template slot-scope="scope">
            <el-button
              type="primary"
              size="mini"
              icon="el-icon-edit"
              @click="handleEdit(scope.row)"
            >编辑</el-button>
            <el-button
              type="danger"
              size="mini"
              icon="el-icon-delete"
              @click="handleDelete(scope.row)"
            >删除</el-button>
            <el-button
              type="warning"
              size="mini"
              icon="el-icon-map-location"
              @click="handleMap(scope.row)"
            >巡更地图</el-button>
          </template>
        </el-table-column>
      </el-table>

      <div class="pagination">
        <el-pagination
          @size-change="handleSizeChange"
          @current-change="handleCurrentChange"
          :current-page="queryParams.pageNum"
          :page-sizes="[10, 20, 30, 50]"
          :page-size="queryParams.pageSize"
          layout="total, sizes, prev, pager, next, jumper"
          :total="total"
        >
        </el-pagination>
      </div>
    </div>
  </div>
</template>

<script>
export default {
  data() {
    return {
      // 查询参数
      queryParams: {
        communityId: null,
        searchText: "",
        pageNum: 1,
        pageSize: 10
      },
      // 社区选项
      communityOptions: [],
      // 表格数据
      tableData: [],
      // 总数据量
      total: 0,
      // 加载状态
      loading: false
    };
  },

  created() {
    // 初始加载数据
    this.loadCommunityOptions();
    this.fetchData();
  },

  methods: {
    // 加载社区选项
    loadCommunityOptions() {
      this.loading = true;
      this.$axios
        .get("api/infrastructure-server/community/queryAll")
        .then(response => {
          if (response.data && response.data.code === 200) {
            this.communityOptions = response.data.data.map(item => {
              return { value: item.id, label: item.name };
            });
          } else {
            this.$message.error(response.data.message || "获取社区列表失败");
          }
        })
        .catch(error => {
          console.error("获取社区列表错误:", error);
          this.$message.error("获取社区列表错误");
        })
        .finally(() => {
          this.loading = false;
        });
    },

    // 获取巡更路线数据
    fetchData() {
      this.loading = true;
      this.$axios
        .post("api/patrol-server/patrolRoute/queryByPageInfo", this.queryParams)
        .then(response => {
          if (response.data && response.data.code === 200) {
            const { records, total } = response.data.data;
            this.tableData = records || [];
            this.total = total || 0;
          } else {
            this.$message.error(response.data.message || "获取巡更路线列表失败");
          }
        })
        .catch(error => {
          console.error("获取巡更路线列表出错:", error);
          this.$message.error("获取巡更路线列表出错");
        })
        .finally(() => {
          this.loading = false;
        });
    },

    // 社区选择变更
    handleCommunityChange() {
      this.searchData();
    },

    // 搜索数据
    searchData() {
      this.queryParams.pageNum = 1;
      this.fetchData();
    },

    // 重置搜索条件
    resetSearch() {
      this.queryParams = {
        communityId: null,
        searchText: "",
        pageNum: 1,
        pageSize: 10
      };
      this.fetchData();
    },

    // 处理页码变化
    handleCurrentChange(val) {
      this.queryParams.pageNum = val;
      this.fetchData();
    },

    // 处理每页显示条数变化
    handleSizeChange(val) {
      this.queryParams.pageSize = val;
      this.queryParams.pageNum = 1;
      this.fetchData();
    },

    // 添加路线
    handleAdd() {
      this.$router.push("/patrolRouteAdd");
    },

    // 编辑路线
    handleEdit(row) {
      this.$router.push({
        path: "/patrolRouteEdit",
        query: { id: row.routeId }
      });
    },

    // 删除路线
    handleDelete(row) {
      this.$confirm("确认删除该巡更路线吗?", "提示", {
        confirmButtonText: "确定",
        cancelButtonText: "取消",
        type: "warning"
      })
        .then(() => {
          this.$axios
            .get(`api/patrol-server/patrolRoute/delete/${row.routeId}`)
            .then(response => {
              if (response.data && response.data.code === 200) {
                this.$message.success("删除成功");
                this.fetchData();
              } else {
                this.$message.error(response.data.message || "删除失败");
              }
            })
            .catch(error => {
              console.error("删除巡更路线出错:", error);
              this.$message.error("删除巡更路线出错");
            });
        })
        .catch(() => {
          // 取消删除操作
        });
    },

    // 查看巡更地图
    handleMap(row) {
      this.$router.push({
        path: "/patrolRouteMap",
         query: { id: row.routeId }
      });
    },
  }
};
</script>

<style scoped>
.patrol-route-container {
  padding: 20px;
  background-color: #f5f7fa;
  min-height: calc(100vh - 60px);
}

.search-bar {
  display: flex;
  justify-content: space-between;
  margin-bottom: 20px;
  background-color: #fff;
  padding: 15px;
  border-radius: 4px;
  box-shadow: 0 2px 12px 0 rgba(0, 0, 0, 0.1);
}

.search-inputs {
  display: flex;
  align-items: center;
  flex-wrap: wrap;
}

.search-item {
  margin-right: 15px;
  width: 200px;
}

.search-buttons {
  display: inline-flex;
}

.table-container {
  background-color: #fff;
  padding: 15px;
  border-radius: 4px;
  box-shadow: 0 2px 12px 0 rgba(0, 0, 0, 0.1);
}

.pagination {
  margin-top: 20px;
  text-align: right;
}

.photo-required {
  color: #67c23a;
}

.photo-not-required {
  color: #909399;
}

.status-normal {
  color: #67c23a;
}

.status-disabled {
  color: #f56c6c;
}
</style>